package extract;

import java.net.*;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.*;
import java.io.*;

public class Main {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {

		List<String>stars =new LinkedList<String>();
		List<String>directors=new LinkedList<String>();
		List<String>writers=new LinkedList<String>();
		List<String>mood=new LinkedList<String>();
		List<String>plot=new LinkedList<String>();
		List<String>genres=new LinkedList<String>();
		List<String>audience=new LinkedList<String>();
		List<String> similars=new LinkedList<String>();
		List<String> rating=new LinkedList<String>();
		
		String[] attributes={"Stars","Directors","Writers","Rating","Mood","Plot","Genres","Audience","Similars"};
		

		String url="http://www.jinni.com/movies/lost";
		URL movie = new URL(url);
		URLConnection yc = movie.openConnection();
		InputStream is= yc.getInputStream();
		
		BufferedReader in = new BufferedReader(
				new InputStreamReader(
						is));
		String inputLine;
		String content="";
		while ((inputLine =in.readLine()) != null) 
			content+=inputLine;
		in.close();

		//content.replace("\n", "");
		//content.replace("\t", "");
		//content.replaceAll("\\s", "");
		System.out.println(content);


		
		//setMovieDetails(stars, directors, writers, content);
		setStars(stars,content);
		setDirectors(directors, content);
		setWriters(writers, content);
		setRating(rating, content);
		setMood(mood,content);
		setPlot(plot,content);
		setGenres(genres,content);
		setAudience(audience,content);
		setSimilars(similars, content);

		for(String s:stars){
			System.out.print(s+",");
		}
		System.out.println();
		for(String s:directors){
			System.out.print(s+",");
		}		
		System.out.println();
		for(String s:writers){
			System.out.print(s+",");
		}
		System.out.println();
		for(String s:mood){
			System.out.print(s+",");
		}
		System.out.println();
		for(String s:plot){
			System.out.print(s+",");
		}
		System.out.println();
		for(String s:genres){
			System.out.print(s+",");
		}
		System.out.println();
		for(String s:audience){
			System.out.print(s+",");
		}
		System.out.println();
		for(String s:similars){
			System.out.print(s+",");
		}
		System.out.println();
	}

/*	private static void setMovieDetails(List<String> stars,
			List<String> directors, List<String> writers, String content) {
		Pattern p = Pattern.compile(RegEx.movieDetails,Pattern.DOTALL);  
		Matcher m = p.matcher(content);   

		Pattern p2=Pattern.compile(RegEx.stars,Pattern.DOTALL);

		//stars, directors and writers
		while(m.find())
		{
			int count=0;
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find())
			{
				for(int i=1;i<=m2.groupCount();i++)
				{
					//System.out.println(m2.group(i));
					switch(count++)
					{
					case 0:
						stars.add(m2.group(i));
						break;
					case 1:
						directors.add(m2.group(i));
						break;
					case 2:
						writers.add(m2.group(i));
						break;
					}
				}
			}

		}
	}
*/
	
	private static void setStars(List<String> stars,String content){
		Pattern p=Pattern.compile(RegEx.stars,Pattern.DOTALL);
		Matcher m=p.matcher(content);
		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find()){
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find()){
				for(int i=1;i<=m2.groupCount();i++){
					stars.add(m2.group(i));
				}
			}
		}
	}
	
	private static void setDirectors(List<String> directors,String content){
		Pattern p=Pattern.compile(RegEx.directors,Pattern.DOTALL);
		Matcher m=p.matcher(content);
		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find()){
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find()){
				for(int i=1;i<=m2.groupCount();i++){
					directors.add(m2.group(i));
				}
			}
		}
	}
	
	private static void setWriters(List<String> writers,String content){
		Pattern p=Pattern.compile(RegEx.writers,Pattern.DOTALL);
		Matcher m=p.matcher(content);
		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find()){
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find()){
				for(int i=1;i<=m2.groupCount();i++){
					writers.add(m2.group(i));
				}
			}
		}
	}
	
	private static void setRating(List<String> rating, String content){
		Pattern p=Pattern.compile(RegEx.rating, Pattern.DOTALL);
		Matcher m=p.matcher(content);

		while(m.find())
		{
			//System.out.println(m.group(1));
			rating.add(m.group(1));
		}
	}

	private static void setMood(List<String>moods, String content){
		Pattern p=Pattern.compile(RegEx.mood,Pattern.DOTALL);
		Matcher m=p.matcher(content);

		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find())
		{
			//System.out.println(m.group(1));
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find())
			{
				for(int i=1;i<=m2.groupCount();i++){
					//System.out.println(m2.group(i));
					moods.add(m2.group(i));
				}
			}

		}
	}

	private static void setPlot(List<String>plot,String content){
		Pattern p=Pattern.compile(RegEx.plot1,Pattern.DOTALL);
		Matcher m=p.matcher(content);

		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find())
		{
			//System.out.println(m.group(1));
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find())
			{
				for(int i=1;i<=m2.groupCount();i++){
					//System.out.println(m2.group(i));
					plot.add(m2.group(i));
				}
			}

		}
		
		p=Pattern.compile(RegEx.plot2,Pattern.DOTALL);
		m=p.matcher(content);
		
		
		while(m.find()){
			//System.out.println(m.group());
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find())
			{
				for(int i=1;i<=m2.groupCount();i++){
					//System.out.println(m2.group(i));
					plot.add(m2.group(i));
				}
			}
		}
	}

	private static void setGenres(List<String>genre,String content){
		Pattern p=Pattern.compile(RegEx.genre,Pattern.DOTALL);
		Matcher m=p.matcher(content);

		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find())
		{
			//System.out.println(m.group(1));
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find())
			{
				for(int i=1;i<=m2.groupCount();i++){
					//System.out.println(m2.group(i));
					genre.add(m2.group(i));
				}
			}

		}
	}

	private static void setAudience(List<String>audience,String content){
		Pattern p=Pattern.compile(RegEx.audience,Pattern.DOTALL);
		Matcher m=p.matcher(content);

		Pattern p2=Pattern.compile(RegEx.fromTagA,Pattern.DOTALL);
		while(m.find())
		{
			//System.out.println(m.group(1));
			Matcher m2=p2.matcher(m.group(1));
			while(m2.find())
			{
				for(int i=1;i<=m2.groupCount();i++){
					//System.out.println(m2.group(i));
					
					
					
					audience.add(m2.group(i));
				}
			}

		}
	}
	
	private static void setSimilars(List<String>similars,String content){
		Pattern p=Pattern.compile(RegEx.similars,Pattern.DOTALL);
		Matcher m=p.matcher(content);
		
		while(m.find()){
			for(int i=1;i<=m.groupCount();i++){
				//System.out.println(m.group(i));
				similars.add(m.group(i));
			}
		}
	}
	
}
